package com.zggk.iroad.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.zggk.iroad.Distribution.distributionlist.DistributionListActivity;
import com.zggk.iroad.R;
import com.zggk.iroad.adapter.recycleview.LGRecycleViewAdapter;
import com.zggk.iroad.adapter.recycleview.LGViewHolder;
import com.zggk.iroad.api.MyAppliCation;
import com.zggk.iroad.check.checklist.CheckListActivity;
import com.zggk.iroad.diease.dieaselist.DieaseListActivity;
import com.zggk.iroad.handle.handlelist.HandleListActivity;
import com.zggk.iroad.log.loglist.LogListActivity;
import com.zggk.iroad.mvp.MVPBaseFragment;
import com.zggk.iroad.util.NoScroolRecycle;
import com.zggk.iroad.view.RadiuImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class HomeFragment extends MVPBaseFragment<HomeContract.View, HomePresenter> implements HomeContract.View {


    @Bind(R.id.gs)
    TextView gs;
    @Bind(R.id.rmd_img)
    RadiuImageView rmdImg;
    @Bind(R.id.rcyh_recycle)
    NoScroolRecycle rcyhRecycle;
    private String xcrzTitle = "巡查日志";
    private String xcbhTitle = "巡查病害";
    private String shpfTitle = "审核派发";
    private String sgwxTitle = "施工维修";
    private String gcysTitle = "养护统计";
    private List<NameBean.ZCDBean> Datas = new ArrayList<>();
    private int[] guide_images = {R.drawable.gcys_img, R.drawable.xcbi_img, R.drawable.shpf_img, R.drawable.sgwx_img, R.drawable.xcrz_img,};
    private LGRecycleViewAdapter<NameBean.ZCDBean> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fra_home, null);
        ButterKnife.bind(this, view);
        setAdapter();
        initData();
        return view;
    }

    private void initData() {
        gs.setText("智能养护巡查手机系统");
        List<NameBean> nameBeans = JSON.parseArray(MyAppliCation.spUtils.getString("ResouRce"), NameBean.class);
        NameBean.ZCDBean zcdBean = new NameBean.ZCDBean();
        zcdBean.setCDMC(xcbhTitle);
        nameBeans.get(0).getZCD().add(zcdBean);
        NameBean.ZCDBean zcdBean2 = new NameBean.ZCDBean();
        zcdBean2.setCDMC(sgwxTitle);
        nameBeans.get(0).getZCD().add(zcdBean2);
        if (nameBeans != null && nameBeans.get(0).getZCD().size() > 0) {
            for (int i = 0; i < nameBeans.get(0).getZCD().size(); i++) {
                String title = nameBeans.get(0).getZCD().get(i).getCDMC();
                if (xcrzTitle.equals(title) || xcbhTitle.equals(title) || shpfTitle.equals(title) || sgwxTitle.equals(title) || gcysTitle.equals(title)) {
                    Datas.add(nameBeans.get(0).getZCD().get(i));
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    private void setAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        rcyhRecycle.setLayoutManager(gridLayoutManager);
        rcyhRecycle.setHasFixedSize(true);
        rcyhRecycle.setNestedScrollingEnabled(false);
        adapter = new LGRecycleViewAdapter<NameBean.ZCDBean>(Datas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_home;
            }

            @Override
            public void convert(LGViewHolder holder, NameBean.ZCDBean zCDBean, int position) {
                if (zCDBean.getCDMC().equals(xcrzTitle)) {
                    holder.setImageResurce(R.id.im, guide_images[4]);
                    holder.setText(R.id.te, "巡查日志");
                } else if (zCDBean.getCDMC().equals(xcbhTitle)) {
                    holder.setImageResurce(R.id.im, guide_images[1]);
                    holder.setText(R.id.te, "病害采集");
                } else if (zCDBean.getCDMC().equals(shpfTitle)) {
                    holder.setImageResurce(R.id.im, guide_images[0]);
                    holder.setText(R.id.te, "审核派发");
                } else if (zCDBean.getCDMC().equals(sgwxTitle)) {
                    holder.setImageResurce(R.id.im, guide_images[3]);
                    holder.setText(R.id.te, "施工维修");
                } else if (zCDBean.getCDMC().equals(gcysTitle)) {
                    holder.setImageResurce(R.id.im, guide_images[2]);
                    holder.setText(R.id.te, "养护统计");
                }
            }
        };
        rcyhRecycle.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                if (Datas.get(position).getCDMC().equals("巡查日志")) {
                    Intent intent = new Intent(getActivity(), LogListActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).getCDMC().equals("巡查病害")) {
                    Intent intent = new Intent(getActivity(), DieaseListActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).getCDMC().equals("审核派发")) {
                    Intent intent = new Intent(getActivity(), DistributionListActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).getCDMC().equals("施工维修")) {
                    Intent intent = new Intent(getActivity(), HandleListActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).getCDMC().equals("养护统计")) {
                    Intent intent = new Intent(getActivity(), CheckListActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onRequestError(String msg) {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}